package zyh.struts2.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements Action {
    private String username;
    private String password;
    private String tip="系统提示：";

    public String getTip() {
        return tip;
    }
    public void setTip(String tip) {
        this.tip = tip;
    }
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public String regist() throws Exception
    {
        ActionContext.getContext().getSession().put("user",getUsername());
        setTip("恭喜你，"+getUsername()+"，您已经注册成功");
        return SUCCESS;
    }
    @Override
    public String execute() throws Exception {
        ActionContext ctx = ActionContext.getContext();
        Integer counter = (Integer) ctx.getApplication().get("counter");
        if (counter == null) {
            counter = 10;
        } else {
            counter = counter + 1;
        }
        ctx.getSession().put("user", getUsername());
//        try
//        {
//            DbDao dd = new DbDao("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/1613580003",
//                    "root", "123456");
//            ResultSet rs = dd.query("select pass from t_users" + " where name = ?", username);
//            while (rs.next())
//            {
//                if (getUsername().equals(username) && getPassword().equals(password)) {
//                    ctx.put(tip, "服务器提示：您已经成功登陆");
//                    return SUCCESS;
//                } else {
//                    ctx.put(tip, "服务器提示：登陆失败");
//                    return ERROR;
//                }
//            }
//        }
//        catch (Exception ex)
//        {
//            ex.printStackTrace();
//        }
        ctx.put("tip", "服务器提示：登陆失败");
        return ERROR;
    }
}
